import type { DataItem, SearchParamsType } from '@/types/table.d';

export const getTableData = (params: SearchParamsType): { [key: string]: any } => {
  const allData: DataItem[] = [];
  for (let i = 0; i < 96; i++) {
    allData.push({
      id: i + 1,
      ruleName: `${i + 1}_这是一条规则名称`,
      describe: `${i + 1}_这是一段描述`,
      frequency: i + 1,
      status: i % 2 === 0 ? 1 : 2,
      useTime: new Date().getTime(),
      createUser: `创建用户${i + 1}`,
      useUser: `调用用户${i + 1}`,
      ip: `192.18.266.${i + 1}`,
    });
  }
  const startIdx = (params.pageNum - 1) * params.pageSize;
  const endIdx = params.pageSize + startIdx;
  const filterDataList = allData.slice(startIdx, endIdx);
  let result: DataItem[] = [];

  if (params.ruleName && params.describe) {
    result = filterDataList.filter((item) => {
      const ruleName = item.ruleName;
      const describe = item.describe;
      return ruleName.includes(params.ruleName || '') || describe.includes(params.describe || '');
    });
  } else if (params.ruleName) {
    result = filterDataList.filter((item) => item.ruleName.includes(params.ruleName || ''));
  } else if (params.describe) {
    result = filterDataList.filter((item) => item.describe.includes(params.describe || ''));
  } else {
    result = filterDataList;
  }

  return {
    result,
    total: params.ruleName || params.describe ? result.length : allData.length,
  };
};
